package com.easylive.app.photo;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.easylive.app.R;
import com.easylive.app.service.MediaService;
import com.stx.xhb.androidx.XBanner;

import java.util.ArrayList;
import java.util.List;

public class PhotoPlayerActivity extends Activity {
    private static final String TAG = "PPA";
    private XBanner mXBanner;
    private BroadcastReceiver mLocalBr = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                String msg = intent.getStringExtra("msg");
                Log.d(TAG, "receive msg:" + msg);
                if ("all_exit".equals(msg) || "video_play".equals(msg)) {
                    finish();
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_player);
        initPhotoPlayer();
        registerMsg();
    }

    private void initPhotoPlayer() {
        mXBanner = findViewById(R.id.view_photo);
        List<CustomViewsInfo> data = new ArrayList<>();
        ArrayList<String> serverData = getIntent().getStringArrayListExtra("data");
        if (serverData != null && !serverData.isEmpty()) {
            for (String url : serverData) {
                data.add(new CustomViewsInfo(url));
            }
        } else {
            data.add(new CustomViewsInfo("https://photo.tuchong.com/250829/f/31548923.jpg"));
            data.add(new CustomViewsInfo("https://photo.tuchong.com/46728/f/20138526.jpg"));
            data.add(new CustomViewsInfo("https://photo.tuchong.com/392724/f/16858773.jpg"));
            data.add(new CustomViewsInfo("https://photo.tuchong.com/408963/f/18401047.jpg"));
        }
        final BannerHolderCreator holderCreator = new BannerHolderCreator();
        mXBanner.setBannerData(data, holderCreator);
        mXBanner.setOnClickListener(view -> {
            MediaService.isPlaying = false;
            finish();
        });
        mXBanner.setOnItemClickListener((banner, model, view, position) -> {
            MediaService.isPlaying = false;
            finish();
        });
    }

    private void registerMsg() {
        IntentFilter intentFilter = new IntentFilter(MediaService.SERVICE_MSG_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBr, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocalBr);
        mXBanner.stopAutoPlay();
    }
}
